package com.originskyseed.platform.base.controller;

import java.util.Arrays;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.DubboReference;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.originskyseed.platform.base.i18n.BaseResponseMsg;
import com.originskyseed.platform.base.model.Logo;
import com.originskyseed.platform.base.service.ILogoService;
import com.originskyseed.platform.base.vo.request.LogoRequestVO;
import com.originskyseed.platform.core.aspect.annotation.RequiredParams;
import com.originskyseed.platform.core.base.BaseController;
import com.originskyseed.platform.core.base.BaseLocaleResource;
import com.originskyseed.platform.core.base.BasePage;
import com.originskyseed.platform.core.base.BaseResponseBody;
import com.originskyseed.platform.core.common.code.CommonResponseCode;
import com.originskyseed.platform.core.filter.helper.SingleSignOnTokenHelper;
import com.originskyseed.platform.user.model.UserInfo;

/**
 * logo信息表Controller类（OSI_LOGO）
 *
 * @author wlj
 */
@Controller
@RequestMapping("/logo")
@BaseLocaleResource(localeFileName = "i18n_base")
public class LogoController extends BaseController {

	private static final Logger logger = LoggerFactory.getLogger(LogoController.class);

	@DubboReference
	private ILogoService logoService;

	/**
	 * 分页查询
	 *
	 * @param request
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/logoList.json", method = RequestMethod.POST)
	@RequiredParams(names = { "pageNo", "pageSize", "type" })
	public BaseResponseBody<BasePage<Logo>> logoList(HttpServletRequest request) {
		BaseResponseBody<BasePage<Logo>> result = null;
		try {
			// 请求对象
			LogoRequestVO requestVO = this.getParameterBean(LogoRequestVO.class);

			// 分页查询
			BasePage<Logo> page = new BasePage<Logo>(requestVO.getPageNo(), requestVO.getPageSize());
			page = this.logoService.selectListByPage(requestVO.toMap(), page);
			result = new BaseResponseBody<BasePage<Logo>>(true, CommonResponseCode.SUCCESS.getCode(), this.getLocaleMessage(this.getClass(), BaseResponseMsg.LOGO_LIST_SUCCESS.getMsg()), page);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			result = new BaseResponseBody<BasePage<Logo>>(false, CommonResponseCode.ERROR.getCode(), this.getLocaleMessage(this.getClass(), BaseResponseMsg.LOGO_LIST_ERROR.getMsg()), e.getMessage(), null);
		}
		return result;
	}

	/**
	 * 获取logo
	 *
	 * @param request
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/getLogo.json", method = RequestMethod.POST)
	@RequiredParams(names = { "type" })
	public BaseResponseBody<Logo> getLogo(HttpServletRequest request) {
		BaseResponseBody<Logo> result = null;
		try {
			long startTime=System.currentTimeMillis();
			// 请求对象
			Logo data = this.getParameterBean(Logo.class);
			data.setStatus(1);
			data.setIsDel(0);
			Logo logo = this.logoService.selectByLogo(data);
			//System.out.println("===getLogo=="+(System.currentTimeMillis()-startTime)/1000+"==="+(System.currentTimeMillis()-startTime)+"==type="+data.getType());
			result = new BaseResponseBody<Logo>(true, CommonResponseCode.SUCCESS.getCode(), this.getLocaleMessage(this.getClass(), BaseResponseMsg.LOGO_SUCCESS.getMsg()), logo);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			result = new BaseResponseBody<Logo>(false, CommonResponseCode.ERROR.getCode(), this.getLocaleMessage(this.getClass(), BaseResponseMsg.LOGO_ERROR.getMsg()), e.getMessage(), null);
		}
		return result;
	}

	/**
	 * 保存信息
	 *
	 * @param request
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/logoSave.json", method = RequestMethod.POST)
	@RequiredParams(names = {})
	public BaseResponseBody<Logo> logoSave(HttpServletRequest request) {
		BaseResponseBody<Logo> result = null;
		try {
			// 请求对象
			Logo logo = this.getParameterBean(Logo.class);
			UserInfo userInfo = SingleSignOnTokenHelper.getCurrentUserInfo(UserInfo.class);

			if (StringUtils.isNotBlank(logo.getId())) {
				if (logo.getStatus() != null && logo.getType() != null && logo.getStatus() != null) {
					if (logo.getType() == 3 && logo.getStatus() == 1) { // 备案信息 启用
						this.logoService.updateStateByIdOnlyOne(0);
					}
				}
				this.logoService.updateByPrimaryKey(logo);
			} else {
				logo.setCreator(userInfo.getName());
				logo.setCreatorId(userInfo.getId());
				logo.setIsDel(0);
				// 把原本的修改为禁用
				if (logo.getStatus() != null && logo.getType() != null && logo.getStatus() != null) {
					if (logo.getType() == 3 && logo.getStatus() == 1) { // 备案信息 启用
						this.logoService.updateStateByIdOnlyOne(0);
					}
				}
				this.logoService.insertSelective(logo);
			}
			result = new BaseResponseBody<Logo>(true, CommonResponseCode.SUCCESS.getCode(), this.getLocaleMessage(this.getClass(), BaseResponseMsg.LOGO_SAVE_SUCCESS.getMsg()), logo);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			result = new BaseResponseBody<Logo>(false, CommonResponseCode.ERROR.getCode(), this.getLocaleMessage(this.getClass(), BaseResponseMsg.LOGO_SAVE_ERROR.getMsg()), e.getMessage(), null);
		}
		return result;
	}

	/**
	 * 删除信息
	 *
	 * @param request
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/logoDelete.json", method = RequestMethod.POST)
	@RequiredParams(names = { "ids" })
	public BaseResponseBody<Boolean> logoDelete(HttpServletRequest request) {
		BaseResponseBody<Boolean> result = null;
		try {
			// 请求参数
			String[] ids = this.getParameterArray("ids", new String[] {});

			this.logoService.updateDeleteByIds(Arrays.asList(ids));
			result = new BaseResponseBody<Boolean>(true, CommonResponseCode.SUCCESS.getCode(), this.getLocaleMessage(this.getClass(), BaseResponseMsg.LOGO_DELETE_SUCCESS.getMsg()), true);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			result = new BaseResponseBody<Boolean>(false, CommonResponseCode.ERROR.getCode(), this.getLocaleMessage(this.getClass(), BaseResponseMsg.LOGO_DELETE_ERROR.getMsg()), e.getMessage(), null);
		}
		return result;
	}

	/**
	 * 修改状态
	 *
	 * @param request
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/logoUpdateState.json", method = RequestMethod.POST)
	@RequiredParams(names = { "ids", "state" })
	public BaseResponseBody<Boolean> logoUpdateState(HttpServletRequest request) {
		BaseResponseBody<Boolean> result = null;
		try {
			// 请求参数
			String[] ids = this.getParameterArray("ids", new String[] {});
			int type = 0;
			if (ids.length > 0) {
				for (String id : ids) {
					Logo logo = this.logoService.selectByPrimaryKey(id);
					type = logo.getType();
					break;
				}
			}
			int state = this.getParameterInteger("state", null);
			this.logoService.updateStateByIds(Arrays.asList(ids), state);
			if (type == 3) {
				this.logoService.updateStateByIdsOnlyOne(Arrays.asList(ids));
			}
			result = new BaseResponseBody<Boolean>(true, CommonResponseCode.SUCCESS.getCode(), this.getLocaleMessage(this.getClass(), BaseResponseMsg.LOGO_SAVE_SUCCESS.getMsg()), true);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			result = new BaseResponseBody<Boolean>(false, CommonResponseCode.ERROR.getCode(), this.getLocaleMessage(this.getClass(), BaseResponseMsg.LOGO_SAVE_ERROR.getMsg()), e.getMessage(), null);
		}
		return result;
	}

}
